﻿var forumURL  = 'http://phimso.vn'; // Ko có dấu "/" ở cuối
var http = createRequestObject();
var field = '';
var loadingText = "<center><img src='newtemp/images/loading.gif'> <b>Đang tải dữ liệu ...</b></center>";

function createRequestObject() {
	var xmlhttp;
	try { xmlhttp=new ActiveXObject("Msxml2.XMLHTTP"); }
	catch(e) {
    try { xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");}
	catch(f) { xmlhttp=null; }
  }
  if(!xmlhttp&&typeof XMLHttpRequest!="undefined") {
	xmlhttp=new XMLHttpRequest();
  }
	return  xmlhttp;
}
function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}


function handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			field.scrollIntoView();
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}

function nohandleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			response = http.responseText;
			field.innerHTML = response;
			if(!response) window.location.href = url;
		}
  	}
	catch(e){}
	finally{}
}

function load_pages(type,num,apr,id,page) { //load_pages('comment',num,film_id,0,1);	
	field = document.getElementById(type);
	if (type != 'comment')	
	field.innerHTML = loadingText;
	http.open('POST',  link_code+'index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('load_pages=1&type='+type+'&num='+num+'&apr='+apr+'&id='+id+'&page='+page); 
  return false; 
}

//#######################################
//# COMMENT
//#######################################
function showComment(num,film_id,page) { 
	field = document.getElementById("comment");
	field.innerHTML = loadingText;
	http.open('POST',  'index.php');
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.onreadystatechange = handleResponse;
    http.send('showcomment=1&num='+num+'&film_id='+film_id+'&page='+page); 
  return false; 
} 

function comment_handleResponse() {
	try {
		if((http.readyState == 4)&&(http.status == 200)){
			var response = http.responseText;
			if (response == 'OK') {
				film_id = encodeURIComponent(document.getElementById("film_id").value);
				num = encodeURIComponent(document.getElementById("num").value);
				showComment(num,film_id,1);
			}
			else document.getElementById("comment_loading").innerHTML = response;

		}
  	}
	catch(e){}
	finally{}
}

function comment_check_values() {
	film_id = encodeURIComponent(document.getElementById("film_id").value);
	num = encodeURIComponent(document.getElementById("num").value);
	//comment_poster = encodeURIComponent(document.getElementById("comment_poster").value);
	comment_content = encodeURIComponent(document.getElementById("comment_content").value);
   // comment_img = encodeURIComponent(document.getElementById("comment_img").value);
	try {
	    document.getElementById("comment_loading").innerHTML = loadingText;
		document.getElementById("comment_loading").style.display = "block";
		http.open('POST',  'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
		http.onreadystatechange = comment_handleResponse;
	//	http.send('comment=1&film_id='+film_id+'&num='+num+'&comment_poster='+comment_poster+'&comment_content='+comment_content);
		http.send('comment=1&film_id='+film_id+'&num='+num+'&comment_content='+comment_content+'&comment_img='+comment_img);
		document.getElementById("submit").disabled=true;document.getElementById("submit").value="Wait...";
	}
	catch(e){}
	finally{}
  return false;
}

//#######################################
//# RATING
//#######################################

function rating(film_id,star) {

   		field = document.getElementById("rating_field");

		field.innerHTML = loadingText;

   		http.open('POST',  'index.php');

   		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');

   		http.onreadystatechange = handleResponse;

		http.send('rating=1&film_id='+film_id+'&star='+star);

 	return false;

}

	// pre-fetch image

	(new Image()).src = RATE_OBJECT_IMG;

	(new Image()).src = RATE_OBJECT_IMG_HALF;

	(new Image()).src = RATE_OBJECT_IMG_BG;



	function show_star(starNum,rate_text) {

		remove_star();

		document.getElementById("rating_text").innerHTML = rate_text;

		full_star(starNum);

	}

	

	function full_star(starNum) {

		for (var i=0; i < starNum; i++)

			document.getElementById('star'+ (i+1)).src = RATE_OBJECT_IMG;

	}

	function remove_star() {

		for (var i=0; i < 5; i++)

			document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;

	}

	function remove_all_star() {

		for (var i=0; i < 5; i++) document.getElementById('star' + (i+1)).src = RATE_OBJECT_IMG_BG; // RATE_OBJECT_IMG_REMOVED;

		document.getElementById("rating_text").innerHTML = 'Bình Chọn';

	}

	function show_rating_process() {

		if(document.getElementById("rating_process")) document.getElementById("rating_process").style.display = "block";

		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "none";

	}

	function hide_rating_process() {

		document.getElementById("rating_process").style.display = "none";

		if(document.getElementById("rate_s")) document.getElementById("rate_s").style.display = "block";

	}

//#######################################
//# BROKEN
//#######################################
function showBroken(film_id,episode_id) {
	try {
		//document.getElementById("broken_field").innerHTML = loadingText;
		//document.getElementById('broken_field').style.display='block';
		http.open('POST',''+WEB_URL+'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				//document.getElementById("broken_field").innerHTML = http.responseText;
				alert('Đã thông báo link lỗi! Cám ơn bạn!');
			}
		}
		http.send('broken=1&film_id='+film_id+'&episode_id='+episode_id);
	}
	catch(e){}
	finally{}
	return false;
}

//#######################################
//# LIKE								#
//#######################################
function Like(film_id) {
	try {
		document.getElementById('vote_field').style.display='block';
		http.open('POST',''+WEB_URL+'index.php');
		http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http.onreadystatechange = function() {
			if((http.readyState == 4)&&(http.status == 200)){
				//document.getElementById("vote_field").innerHTML = http.responseText;
				alert('Bạn đã bình chọn thành công!');
			}
		}
		http.send('like=1&film_id='+film_id);
	}
	catch(e){}
	finally{}
	return false;
}

//#######################################
//# SEARCH
//#######################################

function trim(a) {
	return a.replace(/^s*(S*(s+S+)*)s*$/, "$1");
}
function do_search() {
(function($) {
	var kw = trim($("#keyword").val());
	var t = $("#search_type").val();
	if(!kw||kw=='Tìm kiếm...'){
		alert('Vui lòng nhập từ khóa tìm kiếm!');
		$("#keyword").focus();
		return false;
	}
	kw = kw.toLowerCase().replace(/à|á|ạ|ả|ã|â|ầ|ấ|ậ|ẩ|ẫ|ă|ằ|ắ|ặ|ẳ|ẵ/g,"a")
        .replace(/è|é|ẹ|ẻ|ẽ|ê|ề|ế|ệ|ể|ễ/g,"e")
        .replace(/ì|í|ị|ỉ|ĩ/g,"i")
        .replace(/ò|ó|ọ|ỏ|õ|ô|ồ|ố|ộ|ổ|ỗ|ơ|ờ|ớ|ợ|ở|ỡ/g,"o")
        .replace(/ù|ú|ụ|ủ|ũ|ư|ừ|ứ|ự|ử|ữ/g,"u")
        .replace(/ỳ|ý|ỵ|ỷ|ỹ/g,"y")
        .replace(/đ/g,"d")
        //.replace(/!|@|%|\^|\*|\(|\)|\+|\=|\<|\>|\?|\/|,|\.|\:|\;|\'| |\"|\&|\#|\[|\]|~|$|_/g,"-")
        .replace(/[àâ]/g,"a").replace(/[éèêë]/g,"e").replace(/[îï]/g,"i")
        .replace(/[ô]/g,"o").replace(/[ùû]/g,"u").replace(/[ñ]/g,"n")
        .replace(/[äæ]/g,"ae").replace(/[öø]/g,"oe").replace(/[ü]/g,"ue")
        .replace(/[ß]/g,"ss").replace(/[å]/g,"aa")
        .replace(/[^-a-z0-9~\s\.:;+=_]/g, '').replace(/[\s\.:;=+]+/g, '+')
		.replace(/-+-/g,"-").replace(/^\-+|\-+$/g,"");
	if (t == 'all')
		window.location.href = homepage+'tim-kiem/'+kw+'/page-1.html';	
})(jQuery);
}

//#######################################
//# GOOGLE
//#######################################

function google(ss){
(function($) {
	var config = {
		siteURL		: ss,	// Change this to your site
		searchSite	: true,
		type		: 'web',
		append		: false,
		perPage		: 8,			// A maximum of 8 is allowed by Google
		page		: 0				// The start page
	}
	
	// The small arrow that marks the active search icon:
	var arrow = $('<span>',{className:'arrow'}).appendTo('ul.icons');
	
	$('ul.icons li').click(function(){
		var el = $(this);
		
		if(el.hasClass('active')){
			// The icon is already active, exit
			return false;
		}
		
		el.siblings().removeClass('active');
		el.addClass('active');
		
		// Move the arrow below this icon
		arrow.stop().animate({
			left		: el.position().left,
			marginLeft	: (el.width()/2)-4
		});
		
		// Set the search type
		config.type = el.attr('data-searchType');
		$('#more').fadeOut();
	});
	
	// Adding the site domain as a label for the first radio button:
	$('#siteNameLabel').append(' '+config.siteURL);
	
	// Marking the Search tutorialzine.com radio as active:
	$('#searchSite').click();	
	
	// Marking the web search icon as active:
	$('li.web').click();
	
	// Focusing the input text box:
	$('#s').focus();

	$('#searchForm').submit(function(){
		googleSearch();
		return false;
	});
	
	$('#searchSite,#searchWeb').change(function(){
		// Listening for a click on one of the radio buttons.
		// config.searchSite is either true or false.
		
		config.searchSite = this.id == 'searchSite';
	});
	
	
	function googleSearch(settings){
		
		// If no parameters are supplied to the function,
		// it takes its defaults from the config object above:
		
		settings = $.extend({},config,settings);
		settings.term = settings.term || $('#s').val();
		
		if(settings.searchSite){
			// Using the Google site:example.com to limit the search to a
			// specific domain:
			settings.term = 'site:'+settings.siteURL+' '+settings.term;
		}
		
		// URL of Google's AJAX search API
		var apiURL = 'http://ajax.googleapis.com/ajax/services/search/'+settings.type+'?v=1.0&callback=?';
		var resultsDiv = $('#resultsDiv');
		
		$.getJSON(apiURL,{q:settings.term,rsz:settings.perPage,start:settings.page*settings.perPage},function(r){
			
			var results = r.responseData.results;
			$('#more').remove();
			
			if(results.length){
				
				// If results were returned, add them to a pageContainer div,
				// after which append them to the #resultsDiv:
				
				var pageContainer = $('<div>',{className:'pageContainer'});
				
				for(var i=0;i<results.length;i++){
					// Creating a new result object and firing its toString method:
					pageContainer.append(new result(results[i]) + '');
				}
				
				if(!settings.append){
					// This is executed when running a new search, 
					// instead of clicking on the More button:
					resultsDiv.empty();
				}
				
				pageContainer.append('<div class="clear"></div>')
							 .hide().appendTo(resultsDiv)
							 .fadeIn('slow');
				
				var cursor = r.responseData.cursor;
				
				// Checking if there are more pages with results, 
				// and deciding whether to show the More button:
				
				if( +cursor.estimatedResultCount > (settings.page+1)*settings.perPage){
					$('<div>',{id:'more'}).appendTo(resultsDiv).click(function(){
						googleSearch({append:true,page:settings.page+1});
						$(this).fadeOut();
					});
				}
			}
			else {
				
				// No results were found for this search.
				
				resultsDiv.empty();
				$('<p>',{className:'notFound',html:'No Results Were Found! (Không tìm thấy kết quả!)'}).hide().appendTo(resultsDiv).fadeIn();
			}
		});
	}
	
	function result(r){
		
		// This is class definition. Object of this class are created for
		// each result. The markup is generated by the .toString() method.
		
		var arr = [];
		
		// GsearchResultClass is passed by the google API
		switch(r.GsearchResultClass){

			case 'GwebSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					//'<a href="',r.unescapedUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GimageSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.unescapedUrl,'" title="',r.titleNoFormatting,'" class="pic" style="width:',r.tbWidth,'px;height:',r.tbHeight,'px;">',
					'<img src="',r.tbUrl,'" width="',r.tbWidth,'" height="',r.tbHeight,'" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.visibleUrl,'</a>',
					'</div>'
				];
			break;
			case 'GvideoSearch':
				arr = [
					'<div class="imageResult">',
					'<a target="_blank" href="',r.url,'" title="',r.titleNoFormatting,'" class="pic" style="width:150px;height:auto;">',
					'<img src="',r.tbUrl,'" width="100%" /></a>',
					'<div class="clear"></div>','<a href="',r.originalContextUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
			case 'GnewsSearch':
				arr = [
					'<div class="webResult">',
					'<h2><a href="',r.unescapedUrl,'" target="_blank">',r.title,'</a></h2>',
					'<p>',r.content,'</p>',
					'<a href="',r.unescapedUrl,'" target="_blank">',r.publisher,'</a>',
					'</div>'
				];
			break;
		}
		
		// The toString method.
		this.toString = function(){
			return arr.join('');
		}
	}
	
	
$('#searchForm').submit();
}) ( jQuery );
}

//#######################################
//# USER LOGIN    						#
//#######################################
function Login_Box() {
               new Boxy('<div class="login_boxy">'+
						'<form action="' + forumURL + '/members/login.html" method="post" name="login_form">'+
						'<table width="100%" border="0" cellpadding="0" cellspacing="5">'+
						'<tr><td class="login_left_boxy">Tài khoản: </td><td class="login_input_top"><input class="login_input_boxy" type="text" name="name" /></td></tr>'+
						'<tr><td class="login_left_boxy">Mật khẩu: </td><td><input class="login_input_boxy" type="password" name="pwd" /></td></tr>'+
						'<tr><td></td><td class="singer_"><input type="checkbox" checked="checked" name="remember" value="1"> Ghi nhớ đăng nhập? | <a href="' + forumURL + '/members/forgot.html">Quên mật khẩu?</a></td></tr>'+
						'<tr><td></td><td class="singer_">Chưa có tài khoản, <a href="' + forumURL + '/members/register.html">Đăng Ký</a></p></td></tr>'+						'<tr><td colspan="2" class="login_bottom_boxy"><input class="login_xxx" type="submit" value="Đăng nhập" name="login"/>&nbsp;<input class="close" type="submit" value="Hủy bỏ" accesskey="s" /></td></tr>'+
						'</table>'+
						'</form></div>', {title: 'Đăng nhập', modal: true});
};
